#include <iostream>
#include <sstream>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <glog/logging.h>

#include "base/camcorder.hh"
#include "base/util.hh"

#if 0
void SplitVideo() {
	// build\bin\Release\video_spliter.exe data\karl\vga\Clown_800\Clown_800_noise_free_L.mp4 data\karl\frame\clown_regular
	// build\bin\Release\video_spliter.exe data\karl\vga\Clown_800\Clown_800_occluded_L.mp4 data\karl\frame\clown_occlusion
	// build\bin\Release\video_spliter.exe data\karl\vga\Clown_800\Clown_800_noisy_L.mp4 data\karl\frame\clown_noisy

	// build\bin\Release\video_spliter.exe data\karl\vga\broccolisoup_800\broccolisoup_800_noise_free_L.mp4 data\karl\frame\broccolisoup_regular
	// build\bin\Release\video_spliter.exe data\karl\vga\broccolisoup_800\broccolisoup_800_occluded_L.mp4 data\karl\frame\broccolisoup_occlusion
	// build\bin\Release\video_spliter.exe data\karl\vga\broccolisoup_800\broccolisoup_800_noisy_L.mp4 data\karl\frame\broccolisoup_noisy

	// build\bin\Release\video_spliter.exe data\karl\vga\cube\cube_noise_free_L.mp4 data\karl\frame\cube_regular
	// build\bin\Release\video_spliter.exe data\karl\vga\cube\cube_occluded_L.mp4 data\karl\frame\cube_occlusion
	// build\bin\Release\video_spliter.exe data\karl\vga\cube\cube_noisy_L.mp4 data\karl\frame\cube_noisy

	// build\bin\Release\video_spliter.exe data\karl\vga\CubeEdgeTexture\CubeEdgeTexture_noise_free_L.mp4 data\karl\frame\cubeedge_regular
	// build\bin\Release\video_spliter.exe data\karl\vga\CubeEdgeTexture\CubeEdgeTexture_occluded_L.mp4 data\karl\frame\cubeedge_occlusion
	// build\bin\Release\video_spliter.exe data\karl\vga\CubeEdgeTexture\CubeEdgeTexture_noisy_L.mp4 data\karl\frame\cubeedge_noisy

	// build\bin\Release\video_spliter.exe data\karl\vga\koalacandy_800\koalacandy_800_noise_free_L.mp4 data\karl\frame\koalacandy_regular
	// build\bin\Release\video_spliter.exe data\karl\vga\koalacandy_800\koalacandy_800_occluded_L.mp4 data\karl\frame\koalacandy_occlusion
	// build\bin\Release\video_spliter.exe data\karl\vga\koalacandy_800\koalacandy_800_noisy_L.mp4 data\karl\frame\koalacandy_noisy

	//std::string video_file = "data/video/slow-motion1.mp4";
	//std::string img_dir = "data/image/slow-motion1/";
	


	//const int id_begein = 0;
	//const int id_end = 120;

	int fid = 0;

	cv::VideoCapture cap(video_file);
	if (!cap.isOpened()) {
		LOG(ERROR) << "can't read video file";
		return -1;
	}
	cv::Mat frame;
	//while (cap.read(frame) && fid < id_end) {
	while (cap.read(frame)) {
		std::stringstream stm;
		stm << img_dir << "/" << std::setw(4) << std::setfill('0') << fid << ".png";
		std::string img_name = stm.str();
		cv::imwrite(img_name, frame);
		fid++;
		std::cout << "cur fid: " << fid << std::endl;
	}
}
#endif

void FuseVideo(const std::string& img_dir) {
	std::cout << "== WIP: " << img_dir << std::endl;
	tk::Camcorder* camcorder = new tk::Camcorder(img_dir+"_prob.avi", 640, 512, true);

	std::vector<std::string> fimgs = tk::getFiles(img_dir+'/', "png", true);

	int fid = 1;
	for (auto fimg : fimgs) {
		cv::Mat img = cv::imread(fimg);
		std::stringstream sstr;
		sstr << "#" << std::setw(4) << std::setfill('0') << fid;
		cv::putText(img, sstr.str(), cv::Point(5, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.0, cv::Scalar(255, 255, 255), 1);
		camcorder->Record(img);
		fid++;
	}

}

int main(int argc, char* argv[]) {
	FuseVideo(argv[1]);
	return 0;
}